# Python主要数据结构及其操作演示

# 1. 列表(List)
print("=== 列表(List)操作 ===")
my_list = [1, 2, 3, "a", "b"]
print("原始列表:", my_list)

# 增
my_list.append("c")  # 末尾添加
my_list.insert(1, "new")  # 指定位置插入
print("添加元素后:", my_list)

# 删
del my_list[0]  # 删除指定位置
my_list.remove("a")  # 删除指定值
popped = my_list.pop()  # 删除并返回末尾元素
print("删除元素后:", my_list, "被删除的元素:", popped)

# 改
my_list[0] = "updated"
print("修改元素后:", my_list)

# 查
print("第二个元素:", my_list[1])
print("是否存在'b':", "b" in my_list)

# 2. 元组(Tuple)
print("\n=== 元组(Tuple)操作 ===")
my_tuple = (1, 2, 3, "a", "b")
print("元组内容:", my_tuple)
print("元组不可变，只能查询:")
print("第三个元素:", my_tuple[2])
print("切片操作:", my_tuple[1:4])

# 3. 字典(Dict)
print("\n=== 字典(Dict)操作 ===")
my_dict = {"name": "Alice", "age": 25, "city": "Beijing"}
print("原始字典:", my_dict)

# 增
my_dict["gender"] = "female"
print("添加键值对后:", my_dict)

# 删
del my_dict["city"]
popped_value = my_dict.pop("age")
print("删除键值对后:", my_dict, "被删除的值:", popped_value)

# 改
my_dict["name"] = "Bob"
print("修改值后:", my_dict)

# 查
print("获取name:", my_dict.get("name"))
print("所有键:", my_dict.keys())
print("所有值:", my_dict.values())

# 4. 集合(Set)
print("\n=== 集合(Set)操作 ===")
my_set = {1, 2, 3, 4, 5}
print("原始集合:", my_set)

# 增
my_set.add(6)
print("添加元素后:", my_set)

# 删
my_set.remove(3)
my_set.discard(10)  # 安全删除，不存在不会报错
print("删除元素后:", my_set)

# 集合运算
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print("并集:", set1 | set2)
print("交集:", set1 & set2)
print("差集:", set1 - set2)

# 5. 字符串(String)
print("\n=== 字符串(String)操作 ===")
my_str = "Hello, Python!"
print("原始字符串:", my_str)

# 增(字符串不可变，只能创建新字符串)
new_str = my_str + " Welcome!"
print("连接字符串:", new_str)

# 查
print("第一个字符:", my_str[0])
print("子串查找:", my_str.find("Python"))
print("切片操作:", my_str[7:13])

# 其他操作
print("大写:", my_str.upper())
print("替换:", my_str.replace("Python", "World"))
print("分割:", my_str.split(", "))

# 6. 其他数据结构
print("\n=== 其他数据结构 ===")
# 队列(queue)
from collections import deque
queue = deque(["a", "b", "c"])
queue.append("d")  # 入队
print("队列:", queue)
print("出队:", queue.popleft())  # 出队

# 堆栈(stack)
stack = [1, 2, 3]
stack.append(4)  # 入栈
print("堆栈:", stack)
print("出栈:", stack.pop())  # 出栈

# 默认字典(defaultdict)
from collections import defaultdict
d_dict = defaultdict(int)
d_dict["a"] += 1
print("默认字典:", dict(d_dict))

# 有序字典(OrderedDict)
from collections import OrderedDict
od = OrderedDict()
od["first"] = 1
od["second"] = 2
print("有序字典:", od)